package com.whalehearing.tingyou.api.controller.system;

import com.whalehearing.tingyou.common.annotation.DataSource;
import com.whalehearing.tingyou.common.core.controller.BaseController;
import com.whalehearing.tingyou.common.core.domain.AjaxResult;
import com.whalehearing.tingyou.common.core.domain.entity.SysRole;
import com.whalehearing.tingyou.common.core.domain.entity.SysUser;
import com.whalehearing.tingyou.common.enums.DataSourceType;
import com.whalehearing.tingyou.common.utils.SecurityUtils;
import com.whalehearing.tingyou.common.utils.StringUtils;
import com.whalehearing.tingyou.system.service.ISysPostService;
import com.whalehearing.tingyou.system.service.ISysRoleService;
import com.whalehearing.tingyou.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息
 * @author zengjun-qxm
 */
@Api(tags="api用户信息管理")
@RestController
@RequestMapping("/api/system/user")
@DataSource(DataSourceType.SLAVE)
public class ApiSysUserController extends BaseController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPostService postService;

    /**
     * 根据用户编号获取详细信息
     */
    @ApiOperation("根据-userId-获取详细信息")
    @GetMapping(value = {"/getInfoByUserId/{userId}"})
    public AjaxResult getInfoByUserId(@PathVariable(value = "userId", required = false) Long userId) {
//        userService.checkUserDataScope(userId);
        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(userId)) {
            SysUser sysUser = userService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
            ajax.put("postIds", postService.selectPostListByUserId(userId));
            ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }
        return ajax;
    }

    @ApiOperation("根据-createBy-获取详细信息")
    @GetMapping(value = {"/getInfoByCreateBy/{createBy}"})
    public String getInfoByCreateBy(@PathVariable(value = "createBy", required = false) String createBy) {
        System.out.println("------根据-createBy-获取详细信息--正在开发中，敬请期待！！！！----------" + createBy);
        return "正在开发中，敬请期待！！！";
    }

    /**
     * 修改用户信息
     */
    @ApiOperation("修改用户信息")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        if (!userService.checkUserNameUnique(user)) {
            return error("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) {
            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) {
            return error("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setUpdateBy(getUsername());
        return toAjax(userService.updateUser(user));
    }

    /**
     * 重置密码
     */
    @ApiOperation("重置密码")
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUser user) {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        return toAjax(userService.resetPwd(user));
    }

}
